
#ifndef XDNS_SERVER_MANAGER_IMPL_H
#define XDNS_SERVER_MANAGER_IMPL_H

#include "xdns-server-manager.h"
#include <map>
#include <vector>

class XdnsQueryState;
class XdnsServerSelector;
class XdnsServerState;

class XdnsServerManagerImpl : public XdnsServerManager
{
public:
    friend class XdnsServerManager;

    /* See xdns-server-manager.h for description. */
    virtual XdnsError SendQuery(XdnsQuery* query,
                                long* queryId);

    /* See xdns.h for description. */
    virtual void CancelQuery(long queryId);
    virtual XdnsError GetProcessingFds(int* nfds,
                                       fd_set* readfds,
                                       fd_set* writefds);
    virtual XdnsError Process(fd_set* readfds,
                              fd_set* writefds);

private:
    /*! XdnsServerManagerImpl constructor.

        @param selector Selector for multiple servers. This function takes
                        the ownership of this pointer.
        @param states Vector of the DNS server states. This function takes
                      the ownership of the XdnsServerState pointers.
    */
    XdnsServerManagerImpl(XdnsServerSelector* selector,
                          std::vector<XdnsServerState*>& states);

    virtual ~XdnsServerManagerImpl();

    XdnsError GetValidQueryId(long& queryId,
                              std::map<long, XdnsQuery*>::iterator& iter);
    long NextQueryId();

    long m_nextQueryId;
    XdnsServerSelector* m_selector;
    std::vector<XdnsServerState*> m_states;
    std::map<long, XdnsQueryState> m_queries;
};

class XdnsQueryState
{
public:
    /*! XdnsQueryState constructor.

        @param query Pointer to the query to send. This function does not
                     take the ownership of this pointer.
    */
    XdnsQueryState(XdnsQuery* query);

    XdnsQueryState(const XdnsQueryState& anotherQueryState);
    ~XdnsQueryState();
    XdnsQueryState& operator=(const XdnsQueryState& anotherQueryState);

    XdnsQuery* m_query;

    // TODO: Save the retry count and failed server states.
};

#endif  // XDNS_SERVER_MANAGER_IMPL_H

